import { Vue, Component, Prop, Provide, Emit, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { CreateElement } from 'vue';
import './bxdwfaction-view.less';

import WFActionViewEngine from '@engine/view/wfaction-view-engine';

import view_form from '@widget/bxd/main-form/main-form';

@Component({
    components: {
        view_form, 
    },
    beforeRouteEnter: (to: any, from: any, next: any) => {
        next((vm: any) => {
            vm.$store.commit('addCurPageViewtag', { fullPath: to.fullPath, viewtag: vm.viewtag });
        });
    },
})
export default class BXDWFActionView extends Vue {

    /**
     * 数据变化
     *
     * @param {*} val
     * @returns {*}
     * @memberof BXDWFActionView
     */
    @Emit() 
    public viewDatasChange(val: any):any {
        return val;
    }

    /**
     * 数据视图
     *
     * @type {string}
     * @memberof BXDWFActionView
     */
    @Prop() public viewdata!: string;

	/**
	 * 视图标识
	 *
	 * @type {string}
	 * @memberof AppDashboardView
	 */
	public viewtag: string = '2f61762e997dcd2d672831f4d27d2374';

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof BXDWFActionView
     */
    protected srfparentdata: any = {};

    /**
     * 视图模型数据
     *
     * @type {*}
     * @memberof BXDWFActionView
     */
    public model: any = {
        srfTitle: '报销单工作流操作视图',
        srfCaption: 'bxd.views.wfactionview.caption',
        srfSubCaption: '',
        dataInfo: ''
    }

    /**
     * 处理值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof BXDWFActionView
     */
    @Watch('viewdata')
    onViewData(newVal: any, oldVal: any) {
        const _this: any = this;
        if (!Object.is(newVal, oldVal) && _this.engine) {
            _this.engine.setViewData(newVal);
            _this.engine.load();
        }
    }

    /**
     * 容器模型
     *
     * @type {*}
     * @memberof BXDWFActionView
     */
    public containerModel: any = {
        view_form: { name: 'form', type: 'FORM' },
        view_okbtn: { name: 'okbtn', type: 'button', text: '确定', disabled: true },
        view_cancelbtn: { name: 'cancelbtn', type: 'button', text: '取消', disabled: false },
        view_leftbtn: { name: 'leftbtn', type: 'button', text: '左移', disabled: true },
        view_rightbtn: { name: 'rightbtn', type: 'button', text: '右移', disabled: true },
        view_allleftbtn: { name: 'allleftbtn', type: 'button', text: '全部左移', disabled: true },
        view_allrightbtn: { name: 'allrightbtn', type: 'button', text: '全部右移', disabled: true },
        wflinks: [],
    };

    /**
     * 视图状态订阅对象
     *
     * @private
     * @type {Subject<{action: string, data: any}>}
     * @memberof BXDWFActionView
     */
    public viewState: Subject<ViewState> = new Subject();


    /**
     * 视图引擎
     *
     * @private
     * @type {Engine}
     * @memberof BXDWFActionView
     */
    private engine: WFActionViewEngine = new WFActionViewEngine();

    /**
     * 引擎初始化
     *
     * @private
     * @memberof BXDWFActionView
     */
    private engineInit(): void {
        this.engine.init({
            view: this,
            form: this.$refs.form,
            p2k: '0',
            isLoadDefault: true,
        });
    }

    /**
     * Vue声明周期
     *
     * @memberof BXDWFActionView
     */
    public created() {
        const secondtag = this.$util.createUUID();
        this.$store.commit('viewaction/createdView', { viewtag: this.viewtag, secondtag: secondtag });
        this.viewtag = secondtag;
        
    }

    /**
     * 销毁之前
     *
     * @memberof BXDWFActionView
     */
    public beforeDestroy() {
        this.$store.commit('viewaction/removeView', this.viewtag);
    }

    /**
     * Vue声明周期(组件初始化完毕)
     *
     * @memberof BXDWFActionView
     */
    public mounted() {
        const _this: any = this;
        _this.engineInit();
        if (_this.loadModel && _this.loadModel instanceof Function) {
            _this.loadModel();
        }
        
    }


    /**
     * form 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof BXDWFActionView
     */
    public form_save($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'save', $event);
    }


    /**
     * form 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof BXDWFActionView
     */
    public form_load($event: any, $event2?: any) {
        this.engine.onCtrlEvent('form', 'load', $event);
    }




    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof BXDWFActionView
     */
    public closeView(args: any[]): void {
        let _view: any = this;
        if (_view.viewdata) {
            _view.$emit('viewdataschange', args);
            _view.$emit('close');
        } else if (_view.$tabPageExp) {
            _view.$tabPageExp.onClose(_view.$route.fullPath);
        }
    }

    /**
     * 确定
     *
     * @memberof BXDWFActionView
     */
    public onClickOk(): void {
        const form: any = this.$refs.form;
        if (!form) {
            return;
        }
        this.$store.commit('viewaction/setViewDataChange', { viewtag: this.viewtag, viewdatachange: false });
        this.$emit('viewdataschange', [{ ...form.data }]);
        this.$emit('close', null);
    }

    /**
     * 取消
     *
     * @memberof BXDWFActionView
     */
    public onClickCancel(): void {
        this.$emit('close', null);
    }


    /**
     * 绘制视图消息 （上方）
     *
     * @returns
     * @memberof BXDWFActionView
     */
    public renderPosTopMsgs() {
        return (
            <div class='view-top-messages'>
            </div>
        );
    }

    /**
     * 绘制视图消息 （下方）
     *
     * @returns
     * @memberof BXDWFActionView
     */
    public renderPosBottomMsgs() {
        return (
            <div class='view-bottom-messages'>
            </div>
        );
    }
    
    /**
     * 绘制内容
     *
     * @param {CreateElement} h
     * @returns
     * @memberof BXDWFActionView
     */
    public render(h: CreateElement) {
        return (
        <div class="view-container bxdwfaction-view">
            <card class='view-card' dis-hover={true} bordered={false}>
        
        <p slot='title'>
            <span class='caption-info'>
                {this.$t(this.model.srfCaption)}
                {
                    this.model.dataInfo && !Object.is(this.model.dataInfo, '') ?
                        ': ' + this.model.dataInfo
                        : ''
                }
            </span>
        </p>
        
                <div class="content-container">
                    {this.renderPosTopMsgs()}
                <view_form 
                    viewState={this.viewState} 
                        autosave={false} 
                    viewtag={this.viewtag} 
                    showBusyIndicator={true} 
                    updateAction='update' 
                    removeAction='remove' 
                    loaddraftAction='getdraft' 
                    loadAction='get' 
                    createAction='create' 
                    style='' 
                 
                    name='form' 
                    ref='form' 
                    on-save={($event: any) => this.form_save($event)} 
                    on-load={($event: any) => this.form_load($event)} 
                    on-closeview={($event: any) => this.closeView($event)}>
                </view_form>
                    {this.renderPosBottomMsgs()}
                     <card dis-hover={true} bordered={false} class='footer'>
                        <row style={{ textAlign: 'right' }}>
                            <i-button type='primary' on-click={() => this.onClickOk()}>{this.containerModel.view_okbtn.text}</i-button>
                                    &nbsp;&nbsp;
                            <i-button on-click={() => this.onClickCancel()}>{this.containerModel.view_cancelbtn.text}</i-button>
                        </row>
                    </card>
                </div>
            </card>
        </div>
        );
    }

}